//myPlugin by Zero

(function( $ ){

//start dialog plugin
  var methods = {
	
	init : function( options ) {
   
            var defaults = {
                            modal: true,
                            width: 500,
                            message: '',
                            buttons: {}
                            
            };
            options = $.extend(defaults, options);
            return this.each(function(){
                obj = $(this);
                title = obj.attr("title");
                
                obj.css("z-index","1000").css("position","absolute");
                obj.width(options.width);
                obj.html('<div class="dialog corner-all shadow" style="width: ' + options.width +'px;">' +
                            '<div class="title corner-all">' +
                                '<h3>' + title +'</h3>' +
                                '<span class="close" >&times;</span>' +
                                '<div style="clear:both"></div>' +
                            '</div>' +
                            '<div class="dialog-body">' +
                                '<p>' + options.message + obj.html() +
                                '</p>' +
                            '</div>' +
                            '<div class="dialog-button">' +
                                '' +
                            '</div>' +
                        '</div>');
                
                obj.removeAttr("title");
                var modcon = $('<div id="modal" class="modal-backdrop"></div>');
                modcon.appendTo('body');
                if(options.modal == true){
                    modcon.show();
                }
                $('.dialog', obj).position({
                            my: 'center',
                            at: 'center',
                            of: '#modal'
                });
                $('.close', obj).click(function(){
                    $(this).dialog('close');
                });
                var self = this,
			hasButtons = false;
                if (typeof options.buttons === 'object' && options.buttons !== null) {
			$.each(options.buttons, function() {
				return !(hasButtons = true);
			});
		}
                if (hasButtons) {
                    $.each(options.buttons, function(name, props) {
                        var button = $('<input type="button" class="btn btn-green" value="'+ name +'" />')
                                        .click(function() {
						props();
                                        })
                                    .appendTo('.dialog-button');  
                                    
                    });
                }
                else{
                     var button = $('<input type="button" class="btn btn-green" value="Ok" />')
                                        .click(function() {
					    $(this).dialog('close');
                                        })
                                    .appendTo('.dialog-button');  
                }
            });
        } //end init
        
        ,
        
        //closing the dialog
        close : function( ) {
   
           $('.dialog', obj).hide();
           $('#modal').hide();
           obj.attr('title', title);
   
   
        } //end close
  };

  $.fn.dialog = function( method ) {
    
    if ( methods[method] ) {
      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.dialog' );
    }    
  
  }; //end dialog plugin
  
  
  
  //start tabs plugin
  $.fn.tabs  = function(options){
            var defaults = {
                            animate: 'none',
            };
            options = $.extend(defaults, options);
            return this.each(function(){
                var obj = $(this);
                obj.css({'border':'1px solid #C9C9C9'});
                obj.addClass("corner-top");
                var headings = $('h2', obj);
                var tabcontent = headings.next();
                headings.hide();
                tabcontent.hide();
                tabcontent.eq(0).show();
                obj.prepend('<ul class="tabs"></ul>');
                headings.each(function(){
                    var label = $(this).text();
                    $("ul.tabs", obj).append("<li>" + label + "</li>");
                });
                $("ul.tabs", obj).addClass("corner-top");
                $("div p", obj).css({'margin':'10px'});
                var tabs = $("ul.tabs li", obj);
                tabs.addClass('corner-top');
                tabs.eq(0).addClass("sel");
                var tabs = $("ul.tabs li", obj);
                tabs.click(function(){
                    
                    if(options.animate == 'slide'){
                        tabs.removeClass("sel");
                        tabcontent.slideUp();
                        $(this).addClass("sel");
                        var current = tabs.index($(this));
                        tabcontent.eq(current).slideDown();
                    }
                    else if(options.animate == 'fade') {
                        tabs.removeClass("sel");
                        tabcontent.hide();
                        $(this).addClass("sel");
                        var current = tabs.index($(this));
                        tabcontent.eq(current).fadeIn(600);
                    }
                    else{
                        tabs.removeClass("sel");
                        tabcontent.hide();
                        $(this).addClass("sel");
                        var current = tabs.index($(this));
                        tabcontent.eq(current).show();
                    }
                    
                    
                    
                });
            });
        }; // end of tabs plugin
        
        
    // start truncate plugin
    
     $.fn.truncate = function(options){
            var defaults = {
                            length: 300,
                            mintrail: 20,
                            moreText: "more",
                            lessText: "less",
                            ellipsisText: "..."
            };
            options = $.extend(defaults, options);
            
            
            return this.each(function(){
                
                obj = $(this);
                var body = obj.html();
                        
                if(body.length > options.length + options.mintrail)
                {
                    
                    var splitLocation = body.indexOf(' ', options.length);
                    if(splitLocation != -1)
                    {
                        var splitLocation = body.indexOf(' ', options.length);
                        
                        var str1 = body.substring(0, splitLocation + 1);
                        var str2 = body.substring(splitLocation, body.length - 1);
                        obj.html(str1 + '<span class="truncate_ellipsis">' + options.ellipsisText + '</span>' + '<span class="truncate_more">' + str2 + '</span>');
                        obj.find('.truncate_more').css("display", "none");
                        obj.append(
                            '<div class="clearnoth" style="display: inline-block;">' +
                            '<a href="#" class="truncate_more_link" >' +
                            options.moreText + '</a>' +
                            '</div>'
                        );
                        var moreLink = $('.truncate_more_link', obj);
                        var moreContent = $('.truncate_more', obj);
                        var ellipsis = $('.truncate_ellipsis', obj);
                        moreLink.click(function(){
                            if(moreLink.text() == options.moreText)
                            {
                                moreContent.fadeIn();
                                moreLink.text(options.lessText);
                                ellipsis.css("display", "none");
                            }
                            else{
                                moreContent.fadeOut();
                                moreLink.text(options.moreText);
                                ellipsis.css("display", "inline");
                            };
                            return false;
                        });
                    };
                };
            });
     }; // emd trucate plugin
     
    //start tooltip plugin
    $.fn.tooltip = function(options) {
 
        //set default options
        var defaults = {
            classWrapper    : 'tooltip',
            hoverDelay      : 300,
            speed           : 'fast',
            distance        : 20,
            padLeft         : 0
        };
 
        //call in the default otions
        var options = $.extend(defaults, options);
 
        //act upon the element that is passed into the design
        return this.each(function(options){
            
            var $this = $(this);
           
            
            // 1. Get the text, create the tooltip and append
            
            $this.hover(function(event) {
                var toolTip = $this.attr('tooltip');
                $('<span class="tooltip shadow corner-all"></span>').text(toolTip)
                    .appendTo('body')
                    .css('top', (event.pageY - 10) + 'px')
                    .css('left', (event.pageX + 20) + 'px')
                    .fadeIn('slow');
                }, function() {
                    $('.tooltip').fadeOut('fast');
            });
        });
    };//end tooltip plugin
     
     
            

})( jQuery );


/* ===========================================================
 * bootstrap-tooltip.js v2.2.2
 * http://twitter.github.com/bootstrap/javascript.html#tooltips
 * Inspired by the original jQuery.tipsy by Jason Frame
 * ===========================================================
 * Copyright 2012 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */


!function ($) {

  "use strict"; // jshint ;_;


 /* TOOLTIP PUBLIC CLASS DEFINITION
  * =============================== */

  var Tooltip = function (element, options) {
    this.init('tooltip', element, options)
  }

  Tooltip.prototype = {

    constructor: Tooltip

  , init: function (type, element, options) {
      var eventIn
        , eventOut

      this.type = type
      this.$element = $(element)
      this.options = this.getOptions(options)
      this.enabled = true

      if (this.options.trigger == 'click') {
        this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
      } else if (this.options.trigger != 'manual') {
        eventIn = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
        eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
        this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
        this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
      }

      this.options.selector ?
        (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
        this.fixTitle()
    }

  , getOptions: function (options) {
      options = $.extend({}, $.fn[this.type].defaults, options, this.$element.data())

      if (options.delay && typeof options.delay == 'number') {
        options.delay = {
          show: options.delay
        , hide: options.delay
        }
      }

      return options
    }

  , enter: function (e) {
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

      if (!self.options.delay || !self.options.delay.show) return self.show()

      clearTimeout(this.timeout)
      self.hoverState = 'in'
      this.timeout = setTimeout(function() {
        if (self.hoverState == 'in') self.show()
      }, self.options.delay.show)
    }

  , leave: function (e) {
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)

      if (this.timeout) clearTimeout(this.timeout)
      if (!self.options.delay || !self.options.delay.hide) return self.hide()

      self.hoverState = 'out'
      this.timeout = setTimeout(function() {
        if (self.hoverState == 'out') self.hide()
      }, self.options.delay.hide)
    }

  , show: function () {
      var $tip
        , inside
        , pos
        , actualWidth
        , actualHeight
        , placement
        , tp

      if (this.hasContent() && this.enabled) {
        $tip = this.tip()
        this.setContent()

        if (this.options.animation) {
          $tip.addClass('fade')
        }

        placement = typeof this.options.placement == 'function' ?
          this.options.placement.call(this, $tip[0], this.$element[0]) :
          this.options.placement

        inside = /in/.test(placement)

        $tip
          .detach()
          .css({ top: 0, left: 0, display: 'block' })
          .insertAfter(this.$element)

        pos = this.getPosition(inside)

        actualWidth = $tip[0].offsetWidth
        actualHeight = $tip[0].offsetHeight

        switch (inside ? placement.split(' ')[1] : placement) {
          case 'bottom':
            tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
            break
          case 'top':
            tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
            break
          case 'left':
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
            break
          case 'right':
            tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
            break
        }

        $tip
          .offset(tp)
          .addClass(placement)
          .addClass('in')
      }
    }

  , setContent: function () {
      var $tip = this.tip()
        , title = this.getTitle()

      $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
      $tip.removeClass('fade in top bottom left right')
    }

  , hide: function () {
      var that = this
        , $tip = this.tip()

      $tip.removeClass('in')

      function removeWithAnimation() {
        var timeout = setTimeout(function () {
          $tip.off($.support.transition.end).detach()
        }, 500)

        $tip.one($.support.transition.end, function () {
          clearTimeout(timeout)
          $tip.detach()
        })
      }

      $.support.transition && this.$tip.hasClass('fade') ?
        removeWithAnimation() :
        $tip.detach()

      return this
    }

  , fixTitle: function () {
      var $e = this.$element
      if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
        $e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
      }
    }

  , hasContent: function () {
      return this.getTitle()
    }

  , getPosition: function (inside) {
      return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
        width: this.$element[0].offsetWidth
      , height: this.$element[0].offsetHeight
      })
    }

  , getTitle: function () {
      var title
        , $e = this.$element
        , o = this.options

      title = $e.attr('data-original-title')
        || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)

      return title
    }

  , tip: function () {
      return this.$tip = this.$tip || $(this.options.template)
    }

  , validate: function () {
      if (!this.$element[0].parentNode) {
        this.hide()
        this.$element = null
        this.options = null
      }
    }

  , enable: function () {
      this.enabled = true
    }

  , disable: function () {
      this.enabled = false
    }

  , toggleEnabled: function () {
      this.enabled = !this.enabled
    }

  , toggle: function (e) {
      var self = $(e.currentTarget)[this.type](this._options).data(this.type)
      self[self.tip().hasClass('in') ? 'hide' : 'show']()
    }

  , destroy: function () {
      this.hide().$element.off('.' + this.type).removeData(this.type)
    }

  }


 /* TOOLTIP PLUGIN DEFINITION
  * ========================= */

  var old = $.fn.tooltip

  $.fn.tooltip = function ( option ) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('tooltip')
        , options = typeof option == 'object' && option
      if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }

  $.fn.tooltip.Constructor = Tooltip

  $.fn.tooltip.defaults = {
    animation: true
  , placement: 'top'
  , selector: false
  , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
  , trigger: 'hover'
  , title: ''
  , delay: 0
  , html: false
  }


 /* TOOLTIP NO CONFLICT
  * =================== */

  $.fn.tooltip.noConflict = function () {
    $.fn.tooltip = old
    return this
  }

}(window.jQuery);

